//
//  AllKindsOfButtonViewController.swift
//  UI
//
//  Created by admin on 14/9/22.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit
let CLEARVIEW = 99
let BUTTON1 = 101
let BUTTON2 = 102


class AllKindsOfButtonViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        var button : UIButton = UIButton(frame: CGRect(origin: CGPoint(x: 10, y: 64), size: CGSize(width: 300, height: 40)))
        button.setTitle("Click Me ", forState: UIControlState.Normal)
        button.setTitle("Clicked!", forState: UIControlState.Highlighted)
        button.backgroundColor = UIColor.grayColor()
        button.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        button.titleLabel!.font = UIFont.boldSystemFontOfSize(12)
        self.view.addSubview(button)
        
        /*
        case Custom // no button type
        case System // standard system button
        
        case DetailDisclosure
        case InfoLight
        case InfoDark
        case ContactAdd
        */
        
        var button_System : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button_System.frame = CGRectMake(10, 104, 40, 40)
        button_System.backgroundColor = UIColor.purpleColor()
        
        var button_DetailDisclosure : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
        button_DetailDisclosure.frame = CGRectMake(50, 104, 40, 40)
        button_DetailDisclosure.backgroundColor = UIColor.redColor()
        
        var button_InfoLight : UIButton = UIButton.buttonWithType(UIButtonType.InfoLight) as UIButton
        button_InfoLight.frame = CGRectMake(90, 104, 40, 40)
        button_InfoLight.backgroundColor = UIColor.whiteColor()
        
        var button_InfoDark : UIButton = UIButton.buttonWithType(UIButtonType.InfoDark) as UIButton
        button_InfoDark.frame = CGRectMake(130, 104, 40, 40)
        button_InfoDark.backgroundColor = UIColor.yellowColor()
        
        var button_ContactAdd : UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton
        button_ContactAdd.frame = CGRectMake(170, 104, 40, 40)
        button_ContactAdd.backgroundColor = UIColor.greenColor()
        
        
        var button_Custom : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        button_Custom.frame = CGRectMake(136, 184, 48, 48)
        
        button_Custom.setBackgroundImage(UIImage(named: "1410424013_button-check_basic_blue"), forState: UIControlState.Normal)
        button_Custom.setBackgroundImage(UIImage(named: "1410424034_button-check_basic_green"), forState: UIControlState.Highlighted)
        
        
        self.view.addSubview(button_System)
        self.view.addSubview(button_DetailDisclosure)
        self.view.addSubview(button_InfoLight)
        self.view.addSubview(button_InfoDark)
        self.view.addSubview(button_ContactAdd)
        self.view.addSubview(button_Custom)
        
        
        
        var button_moreLines : UIButton = UIButton(frame: CGRect(origin: CGPoint(x: 10, y: 232), size: CGSize(width: 300, height: 80)))
        button_moreLines.setTitle("Click Me fffffffffffffffffffffffffffff ffffffffffffffffffdddddddddddddddddddddddd", forState: UIControlState.Normal)
        button_moreLines.setTitle("Click Me", forState: UIControlState.Highlighted)
        button_moreLines.titleLabel!.numberOfLines = 3
        
        button_moreLines.backgroundColor = UIColor.grayColor()
        
        button_moreLines.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        button_moreLines.titleLabel!.font = UIFont.boldSystemFontOfSize(12)
        self.view.addSubview(button_moreLines)
        
        
        //动画按钮
        
        var clearView = UIView(frame: CGRectMake(10, 312, 58, 58))
        clearView.tag = CLEARVIEW
        clearView.backgroundColor = UIColor.clearColor()
        clearView.userInteractionEnabled = true
        self.view.addSubview(clearView)
        
        var button_animation = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 48, height: 48)))
        button_animation.setBackgroundImage(UIImage(named: "1410428084_buttonz-wordpress.png"), forState: UIControlState.Normal)
        button_animation.tag = BUTTON1
        button_animation.addTarget(self, action: "flip:", forControlEvents: UIControlEvents.TouchUpInside)
        button_animation.alpha = 1.0
        clearView.addSubview(button_animation)
        
        var button_animation1 = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 48, height: 48)))
        button_animation1.setBackgroundImage(UIImage(named: "1410428086_buttonz-tumblr.png"), forState: UIControlState.Normal)
        button_animation1.tag = BUTTON2
        button_animation1.addTarget(self, action: "flip:", forControlEvents: UIControlEvents.TouchUpInside)
        clearView.addSubview(button_animation1)
        
    }
    
    func flip(sender : UIButton){
        println("---------\(sender)")
        self.view.viewWithTag(BUTTON1)?.alpha = 1.0
        self.view.viewWithTag(BUTTON2)?.alpha = 1.0
        sender.alpha = 0.0
        
        var trans: UIViewAnimationTransition  = (sender.tag == BUTTON1) ? UIViewAnimationTransition.FlipFromLeft : UIViewAnimationTransition.FlipFromRight
        
        //设置动画
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(NSTimeInterval.abs(1))
        UIView.setAnimationTransition(trans, forView: self.view.viewWithTag(CLEARVIEW)!, cache: true)
        self.view.viewWithTag(CLEARVIEW)?.exchangeSubviewAtIndex(0, withSubviewAtIndex: 1)
        UIView.commitAnimations()
        
    }
    
    func buttonClicked(){
        println("---------------------")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
